/*
 * MainFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.chendd.ui;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import cn.chendd.core.MergeFile;
import cn.chendd.core.SplitFile;
import cn.chendd.utils.CommonUtils;

/**
 *
 * @author  __USER__
 */
public class MainFrame extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form MainFrame */
	public MainFrame() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		tabPanel = new javax.swing.JTabbedPane();
		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		splitFileText = new javax.swing.JTextField();
		jLabel4 = new javax.swing.JLabel();
		startSplitButton = new javax.swing.JButton();
		jLabel5 = new javax.swing.JLabel();
		selectSplitButton = new javax.swing.JButton();
		selectSplitButton.setBounds(20, 20, 80, 30);
		selectSplitButton.addMouseListener(new MouseAdapter() {
			//选择某个文件
			@Override
			public void mouseClicked(MouseEvent e) {
				String filePath = CommonUtils.getFileChooser(MainFrame.this, "请选择要分割的文件：");
				if(filePath != null){
					splitFileText.setText(filePath);
				}
			}
		});
		jPanel2 = new javax.swing.JPanel();
		jLabel6 = new javax.swing.JLabel();
		mergeFileText = new javax.swing.JTextField();
		selectMergeButton = new javax.swing.JButton();
		startMergeButton = new javax.swing.JButton();
		jPanel3 = new javax.swing.JPanel();
		jLabel2 = new javax.swing.JLabel();
		fileSizeBox = new javax.swing.JComboBox();
		jLabel3 = new javax.swing.JLabel();
		fileTypeBox = new javax.swing.JComboBox();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("\u6587\u4ef6\u5206\u5272\u8fd8\u539f");

		jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

		jLabel1.setText("\u9009\u62e9\u6587\u4ef6\uff1a");
		jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(
				30, 20, -1, 40));
		splitFileText.setRequestFocusEnabled(false);
		splitFileText.setEditable(false);
		jPanel1.add(splitFileText,
				new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 30, 170,
						-1));

		jLabel4.setText("\u5f53\u524d\u914d\u7f6e\uff1a");
		jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(
				30, 140, -1, -1));

		startSplitButton.setText("开始分割");
		startSplitButton.addMouseListener(new MouseAdapter() {
			//分割文件
			@Override
			public void mouseClicked(MouseEvent e) {
				String text = splitFileText.getText();
				if(text == null || "".equals(text)){
					JOptionPane.showMessageDialog(null, "请先选择文件！", "警告", JOptionPane.ERROR_MESSAGE);
					return;
				}
				//在同一个目录下创建一个新的文件夹，以文件名字命名
				String fileSize = (String) fileSizeBox.getSelectedItem();
				int size = Integer.parseInt(fileSize.replace("M", ""));
				long blockSize = 1024 * 1024 * size;
				String fileType = (String) fileTypeBox.getSelectedItem();
				boolean order = false;
				if("序号分段".equals(fileType)) {
					order = true;
				} else if("随机乱序".equals(fileType)) {
					order = false;
				}
				SplitFile splitFile = new SplitFile(blockSize , order);
				File srcFile = new File(text);
				String fileName = srcFile.getName();
				int index = fileName.lastIndexOf(".");
				if(index != -1){
					fileName = fileName.substring(0 , index);
				}
				String splitFolder = srcFile.getParent() + File.separator + fileName;//与分割源文件在同一个目录
				try {
					boolean flag = splitFile.split(srcFile, splitFolder);
					if(flag){
						JOptionPane.showMessageDialog(null, "文件分割成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
					}
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "操作出现错误：" + e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
				}
			}
			
		});
		jPanel1.add(startSplitButton,
				new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 80, -1,
						-1));
		jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(
				90, 140, -1, -1));

		selectSplitButton.setText("浏览...");
		jPanel1.add(selectSplitButton,
				new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 30, 70,
						25));

		tabPanel.addTab("\u5206\u5272\u6587\u4ef6", jPanel1);

		jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

		jLabel6.setText("合并文件：");
		jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(
				30, 20, -1, 40));

		mergeFileText.setRequestFocusEnabled(false);
		mergeFileText.setEditable(false);
		jPanel2.add(mergeFileText,
				new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 30, 170,
						-1));

		selectMergeButton.setText("浏览...");
		selectMergeButton.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				String filePath = CommonUtils.getFileChooser(MainFrame.this, "请选择合并的文件：" , "obj");
				if(filePath != null){
					mergeFileText.setText(filePath);
				}
			}
			
		});
		
		jPanel2.add(selectMergeButton,
				new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 30, 70,
						25));

		startMergeButton.setText("开始合并");
		
		startMergeButton.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				String text = mergeFileText.getText();
				if(text == null || "".equals(text)){
					JOptionPane.showMessageDialog(null, "请先选择文件！", "警告", JOptionPane.ERROR_MESSAGE);
					return;
				}
				try {
					MergeFile.mergeFile(text);
					JOptionPane.showMessageDialog(null, "文件合并成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "操作出现错误：" + e1.getMessage() , "错误", JOptionPane.ERROR_MESSAGE);
				}
			}
			
		});
		
		jPanel2.add(startMergeButton,
				new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 80, -1,
						-1));

		tabPanel.addTab("合并文件", jPanel2);

		jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

		jLabel2.setText("\u6587\u4ef6\u5927\u5c0f\uff1a");
		jPanel3.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(
				40, 40, -1, -1));

		fileSizeBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"1M", "10M", "50M", "100M" }));
		fileSizeBox.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				setCurrentModelLabel();
			}
		});
		jPanel3.add(fileSizeBox,
				new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 190,
						-1));

		jLabel3.setText("\u5206\u6bb5\u7c7b\u578b\uff1a");
		jPanel3.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(
				40, 80, -1, -1));

		fileTypeBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"序号分段", "随机乱序" }));
		fileTypeBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setCurrentModelLabel();
			}
		});
		jPanel3.add(fileTypeBox,
				new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 80, 190,
						-1));

		tabPanel.addTab("\u8bbe\u7f6e", jPanel3);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				tabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 372,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				tabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 224,
				Short.MAX_VALUE));

		setCurrentModelLabel();
		
		pack();
	}// </editor-fold>
	
	private void setCurrentModelLabel() {
		//设置默认初始化时显示的当前配置信息
		String fileSize = (String) fileSizeBox.getSelectedItem();
		String fileType = (String) fileTypeBox.getSelectedItem();
		jLabel5.setText("分割大小：" + fileSize + "，分割模式：" + fileType);
	}

	//GEN-END:initComponents

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {

				try {
					//设置JToolbar不用渐变图片，使用传统纯色
					UIManager.put("Toolbar.isPaintPlainBackground",
							Boolean.TRUE);
					//透明
					//					BeautyEyeLNFHelper.frameBorderStyle = 
					//							BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
					//正常
					BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
					BeautyEyeLNFHelper.launchBeautyEyeLNF();

				} catch (Exception e) {
				}

				MainFrame mainFrame = new MainFrame();
				mainFrame.setSize(390, 280);
				
				Image logoImage = new ImageIcon(mainFrame.getClass().getResource("/cn/chendd/images/logo.png")).getImage();
				mainFrame.setIconImage(logoImage);
				mainFrame.setResizable(false);
				mainFrame.setLocationRelativeTo(null);
				mainFrame.setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton selectSplitButton;
	private javax.swing.JButton startSplitButton;
	private javax.swing.JButton selectMergeButton;
	private javax.swing.JButton startMergeButton;
	private javax.swing.JComboBox fileSizeBox;
	private javax.swing.JComboBox fileTypeBox;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JTextField splitFileText;
	private javax.swing.JTextField mergeFileText;
	private javax.swing.JTabbedPane tabPanel;
	// End of variables declaration//GEN-END:variables

}